﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;
using System.Text;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;

namespace SWGeneracionE.Models.Repositories
{

    public class GenericRepository<C, T> : IGenericRepository<T>
        where C : DbContext, new()
        where T : class
    {


        private C _entities;
        public C Context
        {
            get { return _entities; }
            set { _entities = value; }
        }

        public GenericRepository()
        {
            _entities = new C();
        }

        public GenericRepository(C context)
        {
            _entities = context;
        }

        public virtual IQueryable<T> GetAll()
        {
            IQueryable<T> query = _entities.Set<T>();
            return query;
        }

        public virtual List<T> GetAllPaging(ref int count, int startIndex = 0, int pageSize = 0, string sorting = null)
        {
            IQueryable<T> query = _entities.Set<T>();
            query = query.OrderBy(sorting);
            count = query.Count();
            
            return pageSize > 0
                       ? query.Skip(startIndex).Take(pageSize).ToList() //Paging
                       : query.ToList(); //No paging
        }
        
        public IQueryable<T> FindBy(Expression<Func<T, bool>> predicate)
        {
            IQueryable<T> query = _entities.Set<T>().Where(predicate);
            return query;
        }

        public virtual List<T> FindByPaging(Expression<Func<T, bool>> predicate, ref int count, int startIndex = 0, int pageSize = 0, string sorting = null)
        {
            IQueryable<T> query = _entities.Set<T>().Where(predicate);
            query = query.OrderBy(sorting);
            count = query.Count();
            
            return pageSize > 0
                       ? query.Skip(startIndex).Take(pageSize).ToList() //Paging
                       : query.ToList(); //No paging
        }

        public T FindFirst(Expression<Func<T, bool>> predicate)
        {
            T queryF = _entities.Set<T>().FirstOrDefault(predicate);
            return queryF;
        }

        public virtual T Add(T entity)
        {
            return _entities.Set<T>().Add(entity);
        }

        public virtual void Delete(T entity)
        {
            _entities.Set<T>().Remove(entity);
        }

        public virtual void Edit(T entity)
        {
            _entities.Entry(entity).State = System.Data.EntityState.Modified;
        }

        public virtual void Save()
        {
            _entities.SaveChanges();
        }

        public virtual void Detach(T entity)
        {
            ((IObjectContextAdapter)_entities).ObjectContext.Detach(entity);
        }

        private bool disposed = false;
        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                    _entities.Dispose();
            }

            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

    }

}